{% extends "base.html" %}

{% block title %}仪表板 - 管理后台{% endblock %}

{% block content %}
<div class="dashboard-container">
    <!-- 页面标题 -->
    <div class="dashboard-header">
        <h1 class="dashboard-title">数据概览</h1>
        <p class="dashboard-subtitle">实时业务数据监控</p>
    </div>

    <!-- 核心指标卡片 -->
    <div class="metrics-grid">
        <div class="metric-card users">
            <div class="metric-icon">👥</div>
            <div class="metric-content">
                <h3 class="metric-value">{{ stats.total_users }}</h3>
                <p class="metric-label">用户总数</p>
                <div class="metric-change positive">
                    <span class="change-arrow">↗</span>
                    <span class="change-value">+{{ stats.new_users_today }}</span>
                </div>
            </div>
        </div>

        <div class="metric-card products">
            <div class="metric-icon">📦</div>
            <div class="metric-content">
                <h3 class="metric-value">{{ stats.total_products }}</h3>
                <p class="metric-label">商品总数</p>
                <div class="metric-change positive">
                    <span class="change-arrow">↗</span>
                    <span class="change-value">+{{ stats.new_products_today }}</span>
                </div>
            </div>
        </div>

        <div class="metric-card orders">
            <div class="metric-icon">📋</div>
            <div class="metric-content">
                <h3 class="metric-value">{{ stats.total_orders }}</h3>
                <p class="metric-label">订单总数</p>
                <div class="metric-change positive">
                    <span class="change-arrow">↗</span>
                    <span class="change-value">+{{ stats.new_orders_today }}</span>
                </div>
            </div>
        </div>

        <div class="metric-card revenue">
            <div class="metric-icon">💰</div>
            <div class="metric-content">
                <h3 class="metric-value">¥{{ "{:,.2f}".format(stats.total_revenue) }}</h3>
                <p class="metric-label">总收入</p>
                <div class="metric-change positive">
                    <span class="change-arrow">↗</span>
                    <span class="change-value">+¥{{ "{:,.2f}".format(stats.revenue_today) }}</span>
                </div>
            </div>
        </div>

        <div class="metric-card production">
            <div class="metric-icon">🏭</div>
            <div class="metric-content">
                <h3 class="metric-value">{{ stats.total_production_tasks }}</h3>
                <p class="metric-label">生产任务</p>
                <div class="metric-change {% if stats.production_completed_today > 0 %}positive{% else %}neutral{% endif %}">
                    <span class="change-arrow">✓</span>
                    <span class="change-value">{{ stats.production_completed_today }} 完成</span>
                </div>
            </div>
        </div>
    </div>

    <!-- 快速操作区域 -->
    <div class="quick-actions">
        <h2 class="section-title">快速操作</h2>
        <div class="actions-grid">
            <a href="/admin/detailed-reports" class="action-card">
                <div class="action-icon">📊</div>
                <h3>详细报表</h3>
                <p>查看完整数据分析</p>
            </a>
            <a href="/admin/export-reports" class="action-card">
                <div class="action-icon">📥</div>
                <h3>导出报表</h3>
                <p>下载Excel报表</p>
            </a>
            <div class="action-card" onclick="refreshDashboard()">
                <div class="action-icon">🔄</div>
                <h3>刷新数据</h3>
                <p>更新实时数据</p>
            </div>
        </div>
    </div>

    <!-- 最近活动 -->
    <div class="recent-activity">
        <h2 class="section-title">最近活动</h2>
        <div class="activity-list">
            <div class="activity-item">
                <div class="activity-icon order">📋</div>
                <div class="activity-content">
                    <p class="activity-text">新订单 #{{ stats.recent_order_id }} 已创建</p>
                    <p class="activity-time">{{ stats.recent_order_time }}</p>
                </div>
            </div>
            <div class="activity-item">
                <div class="activity-icon user">👤</div>
                <div class="activity-content">
                    <p class="activity-text">新用户 {{ stats.recent_user_name }} 已注册</p>
                    <p class="activity-time">{{ stats.recent_user_time }}</p>
                </div>
            </div>
            <div class="activity-item">
                <div class="activity-icon product">📦</div>
                <div class="activity-content">
                    <p class="activity-text">新商品 {{ stats.recent_product_name }} 已添加</p>
                    <p class="activity-time">{{ stats.recent_product_time }}</p>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.dashboard-container {
    padding: 2rem;
    max-width: 1400px;
    margin: 0 auto;
}

.dashboard-header {
    text-align: center;
    margin-bottom: 3rem;
}

.dashboard-title {
    font-size: 2.5rem;
    font-weight: 700;
    color: #2c3e50;
    margin-bottom: 0.5rem;
}

.dashboard-subtitle {
    font-size: 1.1rem;
    color: #7f8c8d;
    margin-bottom: 0;
}

/* 指标卡片样式 */
.metrics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 1.5rem;
    margin-bottom: 3rem;
}

.metric-card {
    background: white;
    border-radius: 16px;
    padding: 2rem;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
    border: 1px solid #f0f0f0;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    position: relative;
    overflow: hidden;
}

.metric-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 30px rgba(0,0,0,0.12);
}

.metric-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg, var(--card-color) 0%, var(--card-color-light) 100%);
}

.metric-card.users { --card-color: #3498db; --card-color-light: #74b9ff; }
.metric-card.products { --card-color: #2ecc71; --card-color-light: #55efc4; }
.metric-card.orders { --card-color: #e74c3c; --card-color-light: #ff7675; }
.metric-card.revenue { --card-color: #f39c12; --card-color-light: #fdcb6e; }
.metric-card.production { --card-color: #9b59b6; --card-color-light: #a29bfe; }

.metric-icon {
    font-size: 2.5rem;
    margin-bottom: 1rem;
    display: inline-block;
    animation: bounce 2s infinite;
}

@keyframes bounce {
    0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
    40% { transform: translateY(-10px); }
    60% { transform: translateY(-5px); }
}

.metric-value {
    font-size: 2.2rem;
    font-weight: 700;
    color: #2c3e50;
    margin-bottom: 0.5rem;
}

.metric-label {
    font-size: 1rem;
    color: #7f8c8d;
    margin-bottom: 1rem;
}

.metric-change {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
}

.metric-change.positive {
    color: #27ae60;
}

.metric-change.neutral {
    color: #95a5a6;
}

.change-arrow {
    font-weight: bold;
}

/* 快速操作样式 */
.quick-actions {
    margin-bottom: 3rem;
}

.section-title {
    font-size: 1.8rem;
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 1.5rem;
    text-align: center;
}

.actions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1.5rem;
}

.action-card {
    background: white;
    border-radius: 12px;
    padding: 2rem;
    text-align: center;
    text-decoration: none;
    color: inherit;
    box-shadow: 0 2px 15px rgba(0,0,0,0.06);
    border: 1px solid #f0f0f0;
    transition: all 0.3s ease;
    cursor: pointer;
}

.action-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 25px rgba(0,0,0,0.1);
    text-decoration: none;
    color: inherit;
}

.action-icon {
    font-size: 3rem;
    margin-bottom: 1rem;
    display: block;
}

.action-card h3 {
    font-size: 1.3rem;
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 0.5rem;
}

.action-card p {
    color: #7f8c8d;
    margin-bottom: 0;
}

/* 最近活动样式 */
.recent-activity {
    background: white;
    border-radius: 16px;
    padding: 2rem;
    box-shadow: 0 2px 15px rgba(0,0,0,0.06);
    border: 1px solid #f0f0f0;
}

.activity-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.activity-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1rem;
    border-radius: 8px;
    background: #f8f9fa;
    transition: background 0.3s ease;
}

.activity-item:hover {
    background: #e9ecef;
}

.activity-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
}

.activity-icon.order { background: #ffe6e6; color: #e74c3c; }
.activity-icon.user { background: #e6f3ff; color: #3498db; }
.activity-icon.product { background: #e6ffe6; color: #2ecc71; }

.activity-content {
    flex: 1;
}

.activity-text {
    font-weight: 500;
    color: #2c3e50;
    margin-bottom: 0.25rem;
}

.activity-time {
    font-size: 0.9rem;
    color: #7f8c8d;
    margin-bottom: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .dashboard-container {
        padding: 1rem;
    }
    
    .dashboard-title {
        font-size: 2rem;
    }
    
    .metrics-grid {
        grid-template-columns: 1fr;
    }
    
    .actions-grid {
        grid-template-columns: 1fr;
    }
}

/* 加载动画 */
.loading {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid var(--card-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}
</style>

<script>
function refreshDashboard() {
    // 显示加载动画
    const refreshButton = event.target.closest('.action-card');
    const originalContent = refreshButton.innerHTML;
    refreshButton.innerHTML = '<div class="loading"></div><h3>刷新中...</h3><p>请稍候</p>';
    
    // 模拟刷新延迟
    setTimeout(() => {
        location.reload();
    }, 1000);
}

// 页面加载动画
document.addEventListener('DOMContentLoaded', function() {
    const cards = document.querySelectorAll('.metric-card');
    cards.forEach((card, index) => {
        card.style.opacity = '0';
        card.style.transform = 'translateY(30px)';
        setTimeout(() => {
            card.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
            card.style.opacity = '1';
            card.style.transform = 'translateY(0)';
        }, index * 100);
    });
});
</script>
{% endblock %}